package com.ruoyi.course.UtilMCTSCourseScheduling;

import java.util.ArrayList;
import java.util.List;

// MCTS节点类，用于存储状态和统计信息
public class MctsNode {
    private MctsState state;                // 当前节点状态
    private MctsNode parent;                // 父节点
    private List<MctsNode> children;        // 子节点列表
    private int visitCount;                 // 访问次数
    private double totalScore;              // 累计评分
    private String courseClassId;           // 该节点对应的课程ID
    private DataPreprocessor.TimeSlot assignedTimeSlot;      // 该节点分配的时间槽

    // 构造函数
    public MctsNode(MctsState state, MctsNode parent, String courseClassId, DataPreprocessor.TimeSlot timeSlot) {
        this.state = state;
        this.parent = parent;
        this.courseClassId = courseClassId;
        this.assignedTimeSlot = timeSlot;
        this.children = new ArrayList<>();
        this.visitCount = 0;
        this.totalScore = 0.0;
    }

    // Getter和Setter方法
    public MctsState getState() {
        return state;
    }

    public MctsNode getParent() {
        return parent;
    }

    public List<MctsNode> getChildren() {
        return children;
    }

    public int getVisitCount() {
        return visitCount;
    }

    public double getTotalScore() {
        return totalScore;
    }

    public void incrementVisitCount() {
        this.visitCount++;
    }

    public void addToTotalScore(double score) {
        this.totalScore += score;
    }

    public void addChild(MctsNode child) {
        this.children.add(child);
    }

    // 计算UCT值（用于选择阶段）
    public double calculateUCT() {
        if (visitCount == 0) {
            return Double.POSITIVE_INFINITY;
        }
        // UCT公式: 平均得分 + 探索系数 * sqrt(ln(父节点访问次数)/当前节点访问次数)
        return (totalScore / visitCount) +
                1.41 * Math.sqrt(Math.log(parent.getVisitCount()) / visitCount);
    }
}

